import microdonebwjm from './microdonebwjm'
import API from '@/config/api'

const whiteList = [];

const request = (url, data, method = 'POST') => {
	let flag = API.whiteList.some(v => v == url);
	if (!whiteList.some(v => url.includes(v))) {
		uni.showLoading({
			mask: true,
			title: "正在加载中"
		})
	}
	return new Promise((resolve, reject) => {
		let header = {
			"Content-Type": "application/json"
		}
		if (uni.getStorageSync('storage_key')) {
			const res = uni.getStorageSync('storage_key');
			const token = res.token
			header['Authorization'] = token
		}

		uni.request({
			url: url,
			data: flag ? data : {},
			method: method,
			header: header,
			success: res => {
				console.log('NET.request', url, data, res)
				if (res.statusCode == 200) {
					if (res.data.code === "200" || res.data.code === "") {
						resolve(res.data)
					} else {
						if (res.data.code == '50026') {
							uni.showModal({
								title: '温馨提示',
								showCancel: false,
								content: res.data.message,
								success(res) {
									uni.navigateBack()
								}
							})
							reject(res)
							return;
						}
						if (res.data.code == '400024') {
							reject(res)
							return
						}
						res.data.message && uni.showModal({
							title: '温馨提示',
							showCancel: false,
							content: res.data.message
						})
						reject(res)
					}
				} else {
					reject(res)
				}
			},
			fail: res => {
				return reject(res)
			},
			complete: () => {
				if (!whiteList.some(v => url.includes(v))) {
					uni.hideLoading()
				}
			},
		})
	});
}
//不带token接口请求，首页
const request1 = (url, data, method = 'GET') => {
	return new Promise((resolve, reject) => {
		let header = {
			'Content-Type': 'application/json',
			'tenant': 'MDAwMA==',
		}
		uni.request({
			url: url,
			data: data,
			method: method,
			header: header,
			success: res => {
				if (res.data.code === "200" || data.code === "") {
					resolve(res.data)
				} else {
					reject(res)
				}
			},
			fail: res => {
				reject(res)
			}
		})
	});
}

export default {
	request: request,
	request1: request1,
}
